package logic

import (
	"context"
	"errors"
	"home-lister/model/mysql"

	"home-lister/homerpc/homerpc"
	"home-lister/homerpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type HomeDetailLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewHomeDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *HomeDetailLogic {
	return &HomeDetailLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *HomeDetailLogic) HomeDetail(in *homerpc.HomeDetailRequest) (*homerpc.HomeDetailResponse, error) {
	// todo: add your logic here and delete this line

	var homes mysql.Home
	mysql.DB.Debug().Where("id = ?", in.HomeId).Find(&homes)
	if homes.ID == 0 {
		return nil, errors.New("当前房源不存在，请更换")
	}

	return &homerpc.HomeDetailResponse{
		HomeId:  int64(homes.ID),
		Name:    homes.Name,
		Price:   float32(homes.Price),
		Address: homes.Address,
	}, nil
}
